//
//  ZHPullViewController.m
//  LBProjectCategoryViewController_Example
//
//  Created by luckyRoy on 2018/3/20.
//  Copyright © 2018年 luckyBoyRoy@163.com. All rights reserved.
//

#import "ZHPullViewController.h"
#import <LBCategoryPullView.h>
#import <Masonry.h>

@interface ZHPullViewController ()

@property (nonatomic, strong) LBCategoryPullView *pullView;

@property (nonatomic, strong) UIButton *showButton;

@property (nonatomic, copy) NSString *selectedCode;

@end

@implementation ZHPullViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIButton *showButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [showButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    showButton.backgroundColor = [UIColor blueColor];
    [self.view addSubview:showButton];
    showButton.tag = 3;
    showButton.frame = CGRectMake(10, 100+50 * 3,[[UIScreen mainScreen] bounds].size.width-20,200);
    self.showButton = showButton;
    
    [self.view addSubview:self.pullView];
    [self.pullView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.pullView show];
}

- (LBCategoryPullView *)pullView
{
    if (!_pullView) {
        __weak __typeof(&*self)weakSelf = self;
        _pullView = [[LBCategoryPullView alloc]initWithFrame:self.view.bounds categoryCode:self.selectedCode codeType:LBCategoryBizCodeTypePurchase maxSelectedLevel:3 complete:^(NSArray<LBCategoryModel *> *modelArray, id parm) {
            [weakSelf.navigationController popToViewController:self animated:NO];
            if (weakSelf.viewBlock) {
                weakSelf.viewBlock(modelArray, parm);
            }
            [weakSelf showtitleBy:modelArray parm:parm];
            [weakSelf.navigationController popToViewController:self animated:YES];
            weakSelf.selectedCode = [modelArray lastObject].code;
            [weakSelf.pullView dimiss];
        }];
    }
    return _pullView;
}

- (void)showtitleBy:(NSArray *)selectedArray parm:(id)parm
{
    NSMutableString *str = [NSMutableString new];
    
    [selectedArray enumerateObjectsUsingBlock:^(LBCategoryModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj.name.length) {
            [str appendString:obj.name];
        }
    }];
    
    [self.showButton setTitle:str forState:UIControlStateNormal];
    
    if ([parm integerValue]) {
        [self.showButton setTitle:@"全部分类" forState:UIControlStateNormal];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
